package top.hepingan.java.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hepingan.java.common.Result;
import top.hepingan.java.entity.Dish;
import top.hepingan.java.service.DishService;

import java.time.LocalDateTime;

@RequestMapping("/dish")
@RestController
@Slf4j
@RequiredArgsConstructor
public class DishController {

    private final DishService dishService;

    /**
     * 查询
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        return Result.data(dishService.list());
    }

    /**
     * 添加
     * @param dish
     * @return
     */
    @PostMapping("/add")
    public Result add(Dish dish) {
        try {
            dish.setCreatedTime(LocalDateTime.now());
            dishService.save(dish);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }

    @PostMapping("/update")
    public Result update(Dish dish) {
        try {
            dishService.updateDish(dish);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }
}
